package org.nlp2rdf.navigator.benchmark.cli;

import org.aksw.commons.util.strings.StringUtils;
import org.apache.commons.cli.*;
import org.nlp2rdf.navigator.benchmark.Sample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * Created by Claus Stadler
 * Date: Oct 23, 2010
 * Time: 10:28:57 AM
 */
public class CliUtils {
    public static final Logger logger = LoggerFactory.getLogger(CliUtils.class);

    public static Random getRandom(String value)
    {
        Random result;
        value = StringUtils.coalesce(value, "").trim();

        return value.isEmpty()
                ? new Random()
                : new Random(Long.parseLong(value));
    }

    public static CommandLine getGnuCommandLine(Options cliOptions, String[] args) throws ParseException {
        CommandLineParser cliParser = new GnuParser();
        return cliParser.parse(cliOptions, args);
    }

    
    public static ApplicationContext getApplicationContext(CommandLine commandLine, String fileName)
    {
        String springXmlFileName = commandLine.getOptionValue("c");
        springXmlFileName = springXmlFileName == null
                ? ""
                : springXmlFileName.trim();

        if(springXmlFileName.isEmpty()) {
            logger.error("No spring config file given, use -c <spring.xml> -f <sample-file>. (if no -f is given, stdin is used).");
            System.exit(1);
        }

        //ApplicationContext context = new FileSystemXmlApplicationContext(new String[] {springXmlFileName});
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {springXmlFileName});

        return context;
    }

    public static List<Integer> parseIntegerList(String csvList)
    {
        List<Integer> result = new ArrayList<Integer>();
        String[] parts = csvList.split(",");
        for(String part : parts) {
            result.add(Integer.parseInt(part));
        }

        return result;
    }

    /*
    public static void main(String[] args)
            throws Exception
    {
        CommandLineParser cliParser = new GnuParser();
        CommandLine commandLine = cliParser.parse(cliOptions, args);


        String springXmlFileName = commandLine.getOptionValue("c");
        springXmlFileName = springXmlFileName == null
                ? ""
                : springXmlFileName.trim();

        if(springXmlFileName.isEmpty()) {
            logger.error("No spring config file given, use -c <spring.xml> -f <sample-file>. (if no -f is given, stdin is used).");
            System.exit(1);
        }

        //ApplicationContext context = new FileSystemXmlApplicationContext(new String[] {springXmlFileName});
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {springXmlFileName});

        BenchmarkContext con = new BenchmarkContext(context);
        Sample<Resource> sample = BenchmarkUtils.readSample(commandLine.getOptionValue("f"));

        logger.info("Learning based on sample: " + sample);
        EvaluatedDescription description = con.getLearner().learn(sample);
        logger.info(description.getDescription().toKBSyntaxString());
    }*/

    public static void outputSet(Set<?> set) {
        for(Object item : set) {
            System.out.println(item);
        }
    }

    public static void outputSample(Sample<?> sample) {
        for(Object item : sample.getPositives()) {
            System.out.println("+" + item);
        }

        //System.out.println();
        
        for(Object item : sample.getNegatives()) {
            System.out.println("-" + item);
        }
    }
}
